<?php
# 文件名称:session.php
# MetInfo在线反馈系统 
# Copyright (C) 长沙米拓信息技术有限公司 (http://www.metinfo.cn). All rights reserved.
defined( '_SYS' ) or die( 'No direct script access allowed' );
class C_tableSession extends C_table
{
	var $session_id			= null;
	var $time				= null;
	var $userid				= null;
	var $usertype			= null;
	var $username			= null;
	var $gid				= null;
	var $guest				= null;
	var $client_id			= null;
	var $data				= null;
	/**
	 * 初始化数据库对象
	 */
	function C_tableSession( &$db )
	{
		parent::__construct( $db->_table_prefix."session", 'session_id', $db );
		$this->guest 	= 1;
		$this->username = '';
		$this->gid 		= 0;
	}
	function insert($sessionId, $clientId)
	{
		$this->session_id	= $sessionId;
		$this->client_id	= $clientId;
		$this->time = time();
		$ret = $this->_db->insertObject( $this->_tb_name, $this, 'session_id' );
		if( !$ret ) {
			$this->setError(strtolower(get_class( $this ))."::". JText::_( 'store failed' ) ."<br />" . $this->_db->stderr());
			return false;
		} else {
			return true;
		}
	}
	function update( $updateNulls = false )
	{
		$this->time = time();
		$ret = $this->_db->updateObject( $this->_tb_name, $this, 'session_id', $updateNulls );
		if( !$ret ) {
			$this->setError(strtolower(get_class( $this ))."::". JText::_( 'store failed' ) ." <br />" . $this->_db->stderr());
			return false;
		} else {
			return true;
		}
	}
	/**
	 * 销毁用户session变量
	 */
	function destroy($userId, $clientIds = array())
	{
		$clientIds = implode( ',', $clientIds );
		$query = 'DELETE FROM '.$this->_tb_name
			. ' WHERE userid = '. $this->_db->Quote( $userId )
			. ' AND client_id IN ( '.$clientIds.' )'
			;
		$this->_db->setQuery( $query );
		if ( !$this->_db->query() ) {
			$this->setError( $this->_db->stderr());
			return false;
		}
		return true;
	}
	/**
	* 回收过期session
	*/
	function purge( $maxLifetime = 1440 )
	{
		$past = time() - $maxLifetime;
		$query = 'DELETE FROM '. $this->_tb_name .' WHERE ( time < \''. (int) $past .'\' )'; // Index on 'VARCHAR'
		$this->_db->setQuery($query);
		return $this->_db->query();
	}
	function store( $updateNulls=false, $opt=true)
	{
		$k = $this->_tb_key;
		
		if( $this->$k && $opt)
		{
			$ret = $this->_db->updateObject( $this->_tb_name, $this, $this->_tb_key, $updateNulls );
		}
		else
		{
			$ret = $this->_db->insertObject( $this->_tb_name, $this, $this->_tb_key );
		}
		if( !$ret )
		{
			$this->setError(get_class( $this ).'::store failed - '.$this->_db->getErrorMsg());
			return false;
		}
		else
		{
			return true;
		}
	}
	/**
	 * 用户是否存在
	 */
	function exists($userid)
	{
		$query = 'SELECT COUNT(userid) FROM '.$this->_tb_name
			. ' WHERE userid = '. $this->_db->Quote( $userid );
		$this->_db->setQuery( $query );
		if ( !$result = $this->_db->loadResult() ) {
			$this->setError($this->_db->stderr());
			return false;
		}
		return (boolean) $result;
	}
	//删除指定session
	function delete( $oid=null )
	{
		$k = $this->_tb_key;
		if ($oid) {
			$this->$k = $oid;
		}
		$query = 'DELETE FROM '.$this->_db->nameQuote( $this->_tb_name ).
				' WHERE '.$this->_tb_key.' = '. $this->_db->Quote($this->$k);
		$this->_db->setQuery( $query );
		if ($this->_db->query())
		{
			return true;
		}
		else
		{
			$this->setError($this->_db->getErrorMsg());
			return false;
		}
	}
}
# 本程序是一个开源系统,使用时请你仔细阅读使用协议,商业用途请自觉购买商业授权.
# Copyright (C) 长沙米拓信息技术有限公司 (http://www.metinfo.cn). All rights reserved.
?>